package com.dxy.mvc;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 *映射器（包含了大量的网址雨方法对应关系）
 */
public class HandlerMapping {
    private static Map<String,MVCMapping> data = new HashMap<>();
    public static MVCMapping get(String uri){
        return data.get(uri);

    }
    public static void Load(InputStream is){
        Properties properties = new Properties();
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取配置文件中的一个个类
        Collection<Object> values = properties.values();
        for (Object value : values) {
            String ClassName = (String) value;
            try {
                //加载配置文件中描述的每一个类
                Class aClass = Class.forName(ClassName);
                //创建配置文件中描述的每一个类
                Object o = aClass.getConstructor().newInstance();
                //获取类中所有的方法
                Method[] methods = aClass.getMethods();
                for (Method method : methods) {
                    Annotation[] annotation = method.getAnnotations();
                    if (annotation !=null){
                        for (Annotation annotation1 : annotation) {
                            if (annotation1 instanceof ResponseBody){
                                //说明此方法，用于返回字符串给客户端
                                MVCMapping mvcMapping = new MVCMapping(o,method,ResponseType.TEXT);
                                Object put = data.put(((ResponseBody) annotation1).value(), mvcMapping);
                                if (put!=null){
                                    //说明存在了重复的请求地址
                                    throw  new RuntimeException("请求地址重复"+((ResponseBody) annotation1).value());
                                }
                            }else if(annotation1 instanceof ResponseView){
                                //用于返回界面给客户端
                                MVCMapping mvcMapping = new MVCMapping(o,method,ResponseType.VIEW);
                                Object put = data.put(((ResponseView) annotation1).value(), mvcMapping);
                                if (put!=null){
                                    throw  new RuntimeException("请求地址重复"+((ResponseView) annotation1).value());
                                }
                            }


                        }
                    }


                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 映射器映射对象，每一个对像封装了一个方法，用于处理请求
     */
    public static class MVCMapping{
        private Object object;
        private Method method;
        private ResponseType responseType;
        public MVCMapping() {
        }
        public MVCMapping(Object object, Method method, ResponseType responseType) {
            this.object = object;
            this.method = method;
            this.responseType = responseType;
        }
        public Object getObject() {
            return object;
        }
        public void setObject(Object object) {
            this.object = object;
        }
        public Method getMethod() {
            return method;
        }
        public void setMethod(Method method) {
            this.method = method;
        }
        public ResponseType getResponseType() {
            return responseType;
        }
        public void setResponseType(ResponseType responseType) {
            this.responseType = responseType;
        }
    }
}
